package com.fms.player.ui.common
{
	import com.fms.player.mvc.FMSFacade;
	import com.fms.player.util.GlobalVar;
	
	import flash.events.NetStatusEvent;
	import flash.media.Camera;
	import flash.media.Microphone;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	
	import mx.controls.Alert;
	import mx.core.UIComponent;
	
	import spark.components.Group;
	
	
	public class PublishStream extends Group
	{
		private var video:Video;
		private var nc:NetConnection;
		private var ns:NetStream;
		private var cam:Camera;
		private var mic:Microphone;
		private var videoName:String;
		
		public function PublishStream(_url:String, _videoName:String)
		{
			super();
			videoName = _videoName;
			cam = Camera.getCamera();
			mic = Microphone.getMicrophone();
			video = new Video(320, 240);
			video.attachCamera(cam);
			video.x = 20;
			video.y = 20;
			
			var ui:UIComponent = new UIComponent();
			ui.width = 500;
			ui.height = 500;
			ui.addChild(video);
			this.addElement(ui);
			
			nc = new NetConnection();
			nc.connect(_url);
			nc.client = this;
			nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatusHandler);
			
		}
		
		private function onNetStatusHandler(evt:NetStatusEvent):void
		{
			Alert.show(evt.info.code);
			if("NetConnection.Connect.Success"==evt.info.code)
			{
				ns = new NetStream(nc);
				ns.addEventListener(NetStatusEvent.NET_STATUS, onNetStatusHandler);
				ns.attachAudio(mic);
				ns.attachCamera(cam);
				ns.publish(videoName,"record");
			}
		}
		
		public function showmsg(msg:String):void
		{
			Alert.show(msg+"\n");
		}
	}
}